# coding: utf-8

# url 管理器 


class UrlManager(object):
    def __init__(self):
        self.new_urls = set()  # 页面解析后获取的新url
        self.old_urls = set()  # 上一步的url

    def add_new_url(self, url):  # url为弹性url，即从页面中获取的其他链接
        if url is None:
            return
        if url not in self.new_urls and url not in self.old_urls:
            self.new_urls.add(url)

    def add_new_urls(self, urls):  # 批量添加url
        if urls is None or len(urls) == 0:
            return
        for url in urls:
            self.add_new_url(url)

    def has_new_url(self):  # 有新的url
        return len(self.new_urls) != 0

    def get_new_url(self): # 从新url列表中获取最后一个url并返回
        new_url = self.new_urls.pop()
        self.old_urls.add(new_url) # 从新列表中取出url并放入old_urls中
        return new_url
